package com.ybkj.daijia.server.event.listener;

import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.server.event.model.OrderYupaiEvent;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.model.Order;
import com.ybkj.daijia.server.model.SmsTemplet;
import com.ybkj.daijia.server.model.SmsTemplet.SmsType;
import com.ybkj.daijia.server.reactor.ReactorVo;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.SmsService;
import com.ybkj.daijia.server.service.SmsTempletService;
import com.ybkj.daijia.server.websocket.WebSocketSend;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class OrderYupaiEventListener implements ApplicationListener<OrderYupaiEvent> {

    private static final Logger logger = LoggerFactory.getLogger(OrderYupaiEventListener.class);

    @Autowired
    private SmsTempletService smsTempletService;
    @Autowired
    private SmsService smsService;
    @Autowired
    private DriverService driverService;


    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private CompanyService companyService;

    @Override
    @Async
    public void onApplicationEvent(OrderYupaiEvent event) {

        logger.debug("order yupaidan event:{}", "");

        ReactorVo vo = (ReactorVo) event.getSource();
        Order order = vo.getOrder();
        String type = vo.getType();

        Driver driver = driverService.findOne(order.getDriverId());

        //推送数据到浏览器
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("orderID", order.getId());
        jsonObject.put("driverID", driver.getId());
        jsonObject.put("type", "yupaidan");
        Company mainCompany = companyService.getMain();
        WebSocketSend.boardcastToCompany(jsonObject, order.getCompanyId(), mainCompany.getId());

        //发送短信
        if (null != type && "man".equals(type)) {
            Company driverCompany = companyService.findOne(driver.getCompanyId());
            if (settingUtils.get().isSmsMainCompany()) {
                driverCompany = mainCompany;
            }
            SmsTemplet templet = smsService.findByAlias(driverCompany.getId(), SmsType.yupaiOrder);

            if (null != templet && StringUtils.isNotBlank(templet.getContent())) {
                try {
                    DateFormat format = new SimpleDateFormat("HH:mm");
                    String serverTime = format.format(order.getServerTime());
                    //smsService.sendSm(driver.getPhone(), "yupaiOrder", "{\"订单号\":\""+"***"+order.getOrderNumber().substring(8)+"\",\"预约时间\":\""+serverTime+"\",\"预约地点\":\""+order.getFromPlace()+"\",\"客户姓名\":\""+order.getClientName()+"\",\"客户电话\":\""+order.getClientPhone()+"\"}", driverCompany.getId(), 0, driverCompany.getSignName());
                } catch (Exception e) {
                    logger.error("发送短信发生异常：{}", e.getMessage());
                }
            }
        }


    }

}
